//
//  DYESPAuthView.m
//  TerritoryShopkeeper
//
//  Created by 黄德玉 on 2018/7/12.
//  Copyright © 2018年 ZDQK. All rights reserved.
//

#import "DYESPAuthView.h"
#import "DYBaseFormView.h"
#import "DYBaseScrollView.h"
#import <Masonry/Masonry.h>



@interface _DYESPAuthDropDownCell : UITableViewCell

@property (nonatomic, strong) UILabel * titleLabel;

@end

@implementation  _DYESPAuthDropDownCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        _titleLabel = [UILabel new];
        _titleLabel.text = @"text";
        _titleLabel.textColor = [UIColor lightGrayColor];
        _titleLabel.font = [UIFont systemFontOfSize:14];
        _titleLabel.textAlignment = NSTextAlignmentLeft;
        [_titleLabel setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
        _titleLabel.numberOfLines = 1;
        [self addSubview:_titleLabel];
        [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.equalTo(self);
            make.left.equalTo(self).offset(3);
        }];
        UIView * lineView = [UIView new];
        lineView.backgroundColor = [[UIColor lightGrayColor] colorWithAlphaComponent:.5];
        [self addSubview:lineView];
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(self);
            make.height.equalTo(@.5);
        }];
    }
    return self;
}

- (void)loadTitle:(NSString *)title{
    self.titleLabel.text = title;
}

@end


////////////////////////////////////////////
////////////////////////////////////////////

@interface _YDESPAutoDropDownView : UIView<UITableViewDelegate,UITableViewDataSource>{
    NSArray * _titles;
}

@property (nonatomic,strong) UIControl * bgControl;
@property (nonatomic,strong)  UITableView * tableView;
@property (nonatomic,copy) void (^actionBackBlock)(id value);
@property (nonatomic,assign) NSInteger currentSelected;
@end


@implementation _YDESPAutoDropDownView

- (instancetype)init
{
    self = [super init];
    if (self) {
        _titles = @[@"兼职电商",@"套餐-兼职电商",@"合伙人"];
        self.bgControl = [[UIControl alloc] init];
        self.bgControl.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0];
        [self.bgControl addTarget:self action:@selector(bgTouched) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:self.bgControl];
        [self.bgControl mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self);
        }];
        _tableView = [[UITableView alloc] init];
        [_tableView registerClass:[_DYESPAuthDropDownCell class] forCellReuseIdentifier:@"cell"];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.estimatedRowHeight = 40;
        _tableView.layer.borderColor = [[UIColor lightGrayColor] colorWithAlphaComponent:.5].CGColor;
        _tableView.layer.borderWidth = 0.5;
        [self addSubview:self.tableView];
        self.currentSelected = -1;
    }
    return self;
}

- (void)bgTouched{
    [self hide];
}

- (void)hide{
    if (self.actionBackBlock) {
        self.actionBackBlock(@(self.currentSelected));
    }
    [UIView animateWithDuration:.3 animations:^{
        self.bgControl.alpha = 0;
        self.alpha = 0;
    } completion:^(BOOL finished) {
        
    }];
}

+ (_YDESPAutoDropDownView * )showWithRect:(CGRect)rect currentRow:(NSInteger)row callback:(void(^)(id value))block{
    _YDESPAutoDropDownView * dropDownView = [[_YDESPAutoDropDownView alloc] init];
    [[UIApplication sharedApplication].keyWindow addSubview:dropDownView];
    [dropDownView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(dropDownView.superview);
    }];
    dropDownView.currentSelected = row;
    dropDownView.actionBackBlock = block;
    dropDownView.tableView.frame = CGRectMake(rect.origin.x, rect.origin.y + rect.size.height, rect.size.width, 120);
    return dropDownView;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 3;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 40;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    _DYESPAuthDropDownCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    [cell loadTitle: _titles[indexPath.row]];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    self.currentSelected = indexPath.row;
    [self hide];
}

@end

/////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////



@interface _DYESPHeaderView : UIView

@property (nonatomic,strong) UIButton * btn;
@property (nonatomic,strong) UILabel * titleLabel;
@property (nonatomic,strong) UIImageView * arrowImgView;
@property (nonatomic,copy) void(^didHeadrClickedBlock)(void);

@end

@implementation _DYESPHeaderView

- (instancetype)init{
    if (self = [super init]) {
        self.backgroundColor = [UIColor colorWithHex:0xf0f3f5 alpha:1];
        self.arrowImgView = [UIImageView new];
        self.arrowImgView.image = [UIImage imageNamed:@"open"];
        
        
        self.btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.btn addTarget:self action:@selector(btnBclicked) forControlEvents:UIControlEventTouchUpInside];
        self.btn.layer.borderColor = [UIColor colorWithHex:0xdbdbdc alpha:1].CGColor;
        self.btn.layer.borderWidth = .5;
        self.btn.layer.cornerRadius = 3;
        self.btn.layer.masksToBounds = YES;
        [self.btn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        self.btn.backgroundColor = [UIColor whiteColor];
        [self addSubview:self.btn];
        
        self.titleLabel = [UILabel new];
        self.titleLabel.textColor = [UIColor lightGrayColor];
        self.titleLabel.text = @"兼职电商";
        self.titleLabel.font = [UIFont systemFontOfSize:14];
        [self addSubview:self.titleLabel];
        [self addSubview:self.arrowImgView];
        [self _layout];
    }
    return self;
}

- (void)_layout{
    [self.btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(20);
        make.right.equalTo(self).offset(-20);
        make.centerY.equalTo(self);
        make.height.equalTo(@27);
    }];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.btn).offset(3);
        make.top.bottom.equalTo(self.btn);
    }];
    [self.arrowImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.btn).offset(-8);
        make.centerY.equalTo(self.btn);
        make.width.equalTo(@10);
        make.height.equalTo(@10);
    }];
}


- (void)btnBclicked{
    if (self.didHeadrClickedBlock) {
        self.didHeadrClickedBlock();
    }
}




@end



////////////////////////////////////////////////
/////////////////////////////////////////////////

@interface DYESPAuthView(){
    NSArray * _titles;
    NSArray * _placesHolders;
    NSArray * _keyboardTypes;
    NSArray * _isSecur;

}

@property (nonatomic,strong) DYBaseScrollView * scrollView;
@property (nonatomic,strong) NSMutableArray< DYBaseInputUnitView * > * inputs;
@property (nonatomic, strong) _DYESPHeaderView * header;

@property (nonatomic,assign) NSInteger currentRow;

@property (nonatomic,strong) NSArray * headers;
@property (nonatomic,strong) NSArray * urls;


@end

@implementation DYESPAuthView

- (instancetype)init{
    if (self = [super init]) {
        
        _titles = @[@"手机号",@"用户名",@"密码"];
        _headers = @[@"兼职电商",@"套餐-兼职电商",@"合伙人"];
        _urls = @[@"http://www.jzdstg.com/appask/license.php",
                  @"http://tc.jzdstg.com/appask/license.php",
                  @"http://www.dztchhr.com/appask/license.php"];
        _placesHolders = @[@"请输入手机号",@"请输入用户名",@"请输入密码"];
        self.currentURL = _urls[0];
        _keyboardTypes = @[@(UIKeyboardTypeDefault),@(UIKeyboardTypeDefault),@(UIKeyboardTypeDefault)];
        _isSecur = @[@0,@0,@1];
        self.scrollView = [[DYBaseScrollView alloc] init];
        [self addSubview:self.scrollView];
        
        self.header = [[_DYESPHeaderView alloc] init];
        __weak typeof(self)weakSelf = self;
        self.header.didHeadrClickedBlock = ^{
            CGRect rect = [weakSelf.header.btn convertRect:weakSelf.header.btn.bounds toView:nil];
            [_YDESPAutoDropDownView showWithRect:rect currentRow:weakSelf
             .currentRow callback:^(id value) {
                weakSelf.currentRow = [value integerValue];
                 weakSelf.header.titleLabel.text = weakSelf.headers[weakSelf.currentRow];
                 weakSelf.currentURL = weakSelf.urls[weakSelf.currentRow];
                 NSLog(@"%@",weakSelf.currentURL);
            }];
        };
        [self.scrollView.contentView addSubview:self.header];
        
        self.inputs = [NSMutableArray array];
        [_titles enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            DYBaseInputUnitView * unit = [[DYBaseInputUnitView alloc] init];
            [self.inputs addObject:unit];
        }];
        [self.inputs enumerateObjectsUsingBlock:^(DYBaseInputUnitView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            [obj setupTitle:_titles[idx] placeHolder:_placesHolders[idx] keyboardType:[_keyboardTypes[idx] integerValue] isSecur:[_isSecur[idx] integerValue]];
            [self.scrollView.contentView addSubview:obj];
        }];
        [self _layout];
    }
    return self;
}

- (void)_layout{
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
    [self.header mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.equalTo(self.scrollView.contentView);
        make.height.equalTo(@50);
    }];
    [self.inputs enumerateObjectsUsingBlock:^(DYBaseInputUnitView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [obj mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self.scrollView.contentView);
            if (idx == 0) {
                make.top.equalTo(self.header.mas_bottom);
            }else{
                make.top.equalTo(self.inputs[idx -1].mas_bottom);
            }
            if (idx == self.inputs.count - 1) {
                make.bottom.equalTo(self.scrollView.contentView).offset(5);
            }
        }];
    }];
}

- (BOOL)checkInputs{
    __block BOOL isRight = YES;
    [self.inputs enumerateObjectsUsingBlock:^(DYBaseInputUnitView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (obj.textfield.text.length == 0) {
            [MBProgressHUD showError:self title:_placesHolders[idx] code:0 animated:YES];
            * stop = YES;
            isRight = NO;
        }
    }];
    if (isRight) {
        self.datas = [NSMutableDictionary dictionary];
        [self.datas setObject:self.inputs[0].textfield.text forKey:@"Phone"];
        [self.datas setObject:self.inputs[1].textfield.text forKey:@"UserName"];
        [self.datas setObject:self.inputs[2].textfield.text forKey:@"Pass"];
    }
  
    return isRight;
}



@end
